﻿using Infragistics.Win.AppStyling;
using Infragistics.Win.UltraMessageBox;
using OrBitDataModule;
using OrBitDataModule.Common;
using OrBitDataModule.Contract;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrBitDataAcquisition.Dialog
{
    public partial class LoginFrm : Form
    {
        public LoginFrm()
        {
            InitializeComponent();

        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            ultraTextEditor_userName.Focus();
            this.ultraTextEditor_userName.KeyPress += (s, k) =>
            {
                if (k.KeyChar == (char)13)
                {
                    if (!string.IsNullOrWhiteSpace(ultraTextEditor_userName.Text))
                    {
                        this.ultraTextEditor_password.Focus();
                    }
                }
            };
            this.ultraTextEditor_password.KeyPress += (s, k) =>
            {
                if (k.KeyChar == (char)13)
                {
                    if (!string.IsNullOrWhiteSpace(ultraTextEditor_password.Text))
                    {
                        UltraButton_login_Click(null, null);
                    }
                }
            };
        }

        private void UltraButton_login_Click(object sender, EventArgs e)
        {
            try
            {
                ultraButton_login.Enabled = false;
                BeginInvoke(new Action(async () =>
                {
                    string userName = ultraTextEditor_userName.Text;
                    string password = ultraTextEditor_password.Text;
                    var result = await WebApi.Login(userName, password);
                    if (result.Result)
                    {
                        MainForm mainForm = new MainForm();
                        mainForm.Show();
                        this.Hide();
                        SharedData.LoginUserName = userName; ;
                        SharedData.LoginUserPassword = password;
                    }
                    else
                    {
                        //MessageBox.Show(result.Item2, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        UltraMessageBoxManager.Show(this, result.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ultraButton_login.Enabled = true;
                    }
                }));
            }
            catch (Exception ex)
            {
                ultraButton_login.Enabled = true;
                throw ex;
            }
        }

        private void UltraButton_quit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LinkLabel_setting_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new WebApiSetting().Show();
        }
    }
}
